#include "../msgbuf.h"

int32_t main( int32_t argc, char * argv[] )
{
	int count = 0;
	MessageBuffer content;
	struct stat fs;
	
	FILE * fd = NULL;
	fd = fopen( "test.cpp", "r" );

	fstat( fileno( fd ), &fs );
	fprintf( stderr, "test.cpp file size %lu\n", fs.st_size );
	int32_t file_size = fs.st_size;
	
	MessageData * data = content.get_buf();
	for(;;)
	{
		count = fread( data->wr_ptr(), 1, data->wr_length(), fd );
		data->wr_ptr( count );
		file_size -= count;
		if( file_size > 0 )
			data = content.get_buf();
		else
			break;
	}
	
	fprintf( stderr, "MessageData size=%lu data_count=%lu\n", content.size(), content.data_count() );

	FILE * out = fopen( "test_bak.cpp", "w" );
	
	MessageBuffer::iterator iter = content.get_data();
	while( !iter.is_done() )
	{
		MessageData * data = iter.value();
		fwrite( data->rd_ptr(), 1, data->rd_length(), out );
		iter.next();
	}

	fclose( out );
	fclose( fd );
	return 0;
}

